<?php

namespace TableTennis\UsefulInformationBundle\Entity;

use Doctrine\ORM\EntityRepository;

class UsefulInformationRepository extends EntityRepository
{
    public function findAllOrderedByUpdatedAt($limit = 5)
    {
        $query = $this->createQueryBuilder('us')
                      ->addSelect('u.username AS u_username')
                      ->addSelect('m.name AS m_name')
                      ->innerJoin('us.modification_user_id', 'u')
                      ->leftJoin('us.media_id', 'm')
                      ->where('us.is_published = 1')
                      ->orderBy('us.updatedAt', 'DESC')
                      ->setMaxResults($limit)
                      ->getQuery();

        try {
            return $query->getResult($query::HYDRATE_SCALAR);
        } catch (\Doctrine\ORM\NoResultException $e) {
            return null;
        }
    }
}